/**
 * @description: 测试使用Cglib代理
 * @author: gjh
 * @Date Created in 2018/6/15 16:45
 */

import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/** 轮船类*/
class Ship {
    public void travel(){
        System.out.println("轮船正在行驶");
    }

}
class ShipProxy implements MethodInterceptor {
    //通过Enhancer 创建代理对象
    private Enhancer enhancer = new Enhancer();

    //通过Class对象获取代理对象
    public Object getProxy(Class c){
        //设置创建子类的类
        enhancer.setSuperclass(c);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method m, Object[] args, MethodProxy proxy) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("日志开始...");
        //代理类调用父类的方法
        proxy.invokeSuper(obj, args);
        System.out.println("日志结束...");
        return null;
    }
}
public class TetsCglib {

    public static void main(String[] args) {
        //创建我们的代理类
        ShipProxy Proxy = new ShipProxy();
        Ship ship = (Ship)Proxy.getProxy(Ship.class);
        ship.travel();

    }
}
